// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Air Bubbler Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 44 : TYPE_AIRBUBBLER

// Function declarations
reserve function AirBubbler_DebugDraw
reserve function AirBubbler_DebugSpawn

// Static Values

// Tables

private table AirBubbler_table27
	2, 4, 2, 2, 2, 2, 4, 2, 4, 2, 2, 4, 2, 4, 2, 2, 4, 2
end table

function AirBubbler_DebugDraw
	DrawSprite(0)
end function


function AirBubbler_DebugSpawn
	CreateTempObject(TypeName[Air Bubbler], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
	object[tempObjectPos].value4 = 0
end function


event ObjectMain
	object.frame++
	object.frame %= 48
	if object.iypos > stage.waterLevel
		temp0 = object.ixpos
		temp0 -= screen.cameraX
		Abs(temp0)
		if temp0 < screen.xcenter
			temp0 = object.iypos
			temp0 -= screen.cameraY
			Abs(temp0)
			if temp0 < screen.ycenter
				temp1 = 0
				if object.value3 == 0
					object.value0--
					if object.value0 < 0
						SetBit(object.value3, 0, 1)
						Rand(object.value2, 0xFFFF)
						object.value1 = object.value2
						object.value2 %= 6
						object.value1 &= 12
						object.value4--
						if object.value4 < 0
							object.value4 = object.propertyValue
							SetBit(object.value3, 1, 1)
						end if
						temp1 = 1
					end if
				else
					object.value0--
					if object.value0 < 0
						temp1 = 1
					end if
				end if
				if temp1 == 1
					Rand(object.value0, 32)
					Rand(temp0, 17)
					temp0 -= 8
					temp0 <<= 16
					temp0 += object.xpos
					CreateTempObject(TypeName[Air Bubble], 2, temp0, object.ypos)
					object[tempObjectPos].value1 = object[tempObjectPos].xpos
					Rand(object[tempObjectPos].angle, 256)
					object[tempObjectPos].angle <<= 1
					object[tempObjectPos].drawOrder = 4
					object[tempObjectPos].yvel = -0x8800
					object[tempObjectPos].value2 = 0xFFFF
					temp0 = object.value1
					temp0 += object.value2
					GetTableValue(object[tempObjectPos].propertyValue, temp0, AirBubbler_table27)
					GetBit(temp0, object.value3, 1)
					if temp0 == 1
						Rand(temp0, 4)
						temp1 = 0
						if temp0 == 0
							GetBit(temp0, object.value3, 2)
							if temp0 == 0
								temp1 = 1
							end if
						end if
						if object.value2 == 0
							GetBit(temp0, object.value3, 2)
							if temp0 == 0
								temp1 = 1
							end if
						end if
						if temp1 == 1
							object[tempObjectPos].propertyValue = 6
							SetBit(object.value3, 2, 1)
						end if
					end if
					object.value2--
					if object.value2 < 0
						Rand(temp0, 128)
						temp0 += 128
						object.value0 += temp0
						object.value3 = 0
					end if
				end if
			end if
		end if
	end if
end event


event ObjectDraw
	if object.iypos >= stage.waterLevel
		temp0 = object.frame
		temp0 >>= 4
		DrawSprite(temp0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 61, 51)
	SpriteFrame(-8, -8, 16, 16, 78, 51)
	SpriteFrame(-8, -8, 16, 16, 95, 51)
	foreach (TypeName[Air Bubbler], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
		object[arrayPos0].value4 = object[arrayPos0].propertyValue
	next
	SetTableValue(TypeName[Air Bubbler], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(AirBubbler_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(AirBubbler_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
